/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dialogs.obra;

import dao.DaoObra;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import libcontrol.model.Obra;

/**
 *
 * @author filipe
 */
public class JDialogListarObra extends javax.swing.JDialog {

    /**
     * Creates new form JDialogListarObra
     */
    public JDialogListarObra(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        preencherTabela(new DaoObra().list());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jTextFieldBuscarTitulo = new javax.swing.JTextField();
        jButtonBuscar = new javax.swing.JButton();
        jButtonCancelar = new javax.swing.JButton();
        jButtonVerObra = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTableListarObra = new javax.swing.JTable();
        jTextFieldBuscarAutor = new javax.swing.JTextField();
        jTextFieldBuscarEdicao = new javax.swing.JTextField();
        jTextFieldBuscarEditora = new javax.swing.JTextField();
        JLabelTitulo = new javax.swing.JLabel();
        JLabelAutor = new javax.swing.JLabel();
        JLabelEdicao = new javax.swing.JLabel();
        JLabelEditora = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jButtonBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/pesquisar.png"))); // NOI18N
        jButtonBuscar.setText("Buscar");
        jButtonBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonBuscarActionPerformed(evt);
            }
        });

        jButtonCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/cancelar.png"))); // NOI18N
        jButtonCancelar.setText("Cancelar");
        jButtonCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelarActionPerformed(evt);
            }
        });

        jButtonVerObra.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/ver.png"))); // NOI18N
        jButtonVerObra.setText("Ver Obra");
        jButtonVerObra.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonVerObraActionPerformed(evt);
            }
        });

        jTableListarObra.setFont(new java.awt.Font("Arial Black", 0, 12)); // NOI18N
        jTableListarObra.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Nome", "Autor", "Edição", "Editora", "Ano", "Categoria"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.Integer.class, java.lang.String.class, java.lang.Integer.class, java.lang.Object.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTableListarObra.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(jTableListarObra);

        JLabelTitulo.setFont(new java.awt.Font("Arial Black", 0, 14)); // NOI18N
        JLabelTitulo.setText("Título");

        JLabelAutor.setFont(new java.awt.Font("Arial Black", 0, 14)); // NOI18N
        JLabelAutor.setText("Autor");

        JLabelEdicao.setFont(new java.awt.Font("Arial Black", 0, 14)); // NOI18N
        JLabelEdicao.setText("Edição");

        JLabelEditora.setFont(new java.awt.Font("Arial Black", 0, 14)); // NOI18N
        JLabelEditora.setText("Editora");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jButtonCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButtonVerObra, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(JLabelTitulo)
                            .addComponent(JLabelEdicao))
                        .addGap(43, 43, 43)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jTextFieldBuscarEdicao, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(26, 26, 26)
                                .addComponent(JLabelEditora)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jTextFieldBuscarEditora))
                            .addComponent(jTextFieldBuscarTitulo, javax.swing.GroupLayout.PREFERRED_SIZE, 333, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(31, 31, 31)
                                .addComponent(JLabelAutor)
                                .addGap(18, 18, 18)
                                .addComponent(jTextFieldBuscarAutor))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addGap(319, 319, 319)
                                .addComponent(jButtonBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addGap(27, 27, 27))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(46, 46, 46)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldBuscarTitulo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextFieldBuscarAutor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(JLabelTitulo)
                    .addComponent(JLabelAutor))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jTextFieldBuscarEdicao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jTextFieldBuscarEditora, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(JLabelEdicao)
                        .addComponent(JLabelEditora))
                    .addComponent(jButtonBuscar))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 248, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonCancelar)
                    .addComponent(jButtonVerObra))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelarActionPerformed
        dispose();
    }//GEN-LAST:event_jButtonCancelarActionPerformed

    private void jButtonBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonBuscarActionPerformed
        String autor = jTextFieldBuscarAutor.getText();
        String edicao = jTextFieldBuscarEdicao.getText();
        String editora = jTextFieldBuscarEditora.getText();
        String titulo = jTextFieldBuscarTitulo.getText();
        List<Obra> obras;
        if (edicao.isEmpty()) {
            obras = new DaoObra().listByTituloAndAutorAndEditora(titulo, autor, editora);
        } else {
            obras = new DaoObra().listByTituloAndAutorAndEdicaoAndEditora(titulo, autor, Integer.parseInt(edicao), editora);
        }
        if (obras.isEmpty()) {
            JOptionPane.showMessageDialog(this, "Não foram encontradas obras para a pesquisa!", "Erro", JOptionPane.ERROR_MESSAGE);
        } else {
            preencherTabela(obras);
        }
    }//GEN-LAST:event_jButtonBuscarActionPerformed

    private void jButtonVerObraActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonVerObraActionPerformed
        int selectedRow = jTableListarObra.getSelectedRow();
        if (selectedRow != -1) {
            String titulo = (String) jTableListarObra.getModel().getValueAt(selectedRow, 0);
            String autor = (String) jTableListarObra.getModel().getValueAt(selectedRow, 1);
            int edicao = (int) jTableListarObra.getModel().getValueAt(selectedRow, 2);
            String editora = (String) jTableListarObra.getModel().getValueAt(selectedRow, 3);
            Obra obra = new DaoObra().findByTituloAndAutorAndEdicaoAndEditora(titulo, autor, edicao, editora);
            if (obra != null) {
                new JDialogVerObra((JFrame) getParent(), true, obra).setVisible(true);
                preencherTabela(new DaoObra().list());
            }
        } else {
            JOptionPane.showMessageDialog(this, "Nenhuma Obra Selecionada!", "Erro", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_jButtonVerObraActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel JLabelAutor;
    private javax.swing.JLabel JLabelEdicao;
    private javax.swing.JLabel JLabelEditora;
    private javax.swing.JLabel JLabelTitulo;
    private javax.swing.JButton jButtonBuscar;
    private javax.swing.JButton jButtonCancelar;
    private javax.swing.JButton jButtonVerObra;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTableListarObra;
    private javax.swing.JTextField jTextFieldBuscarAutor;
    private javax.swing.JTextField jTextFieldBuscarEdicao;
    private javax.swing.JTextField jTextFieldBuscarEditora;
    private javax.swing.JTextField jTextFieldBuscarTitulo;
    // End of variables declaration//GEN-END:variables

    private void preencherTabela(List<Obra> obras) {
        DefaultTableModel model = (DefaultTableModel) jTableListarObra.getModel();
        model.setNumRows(0);
        Object[] rowData = new Object[6];
        for (Obra obra : obras) {
            if (!obra.isExcluida()) {
                rowData[0] = obra.getTitulo();
                rowData[1] = obra.getAutor();
                rowData[2] = obra.getEdicao();
                rowData[3] = obra.getEditora();
                rowData[4] = obra.getAno();
                rowData[5] = obra.getCategoria();
                model.addRow(rowData);
            }
        }
        jTableListarObra.setModel(model);
    }
}
